package wait_signal;

public class SimpleWN {
    static Object object = new Object();

    static class T1 implements Runnable {
        @Override
        public void run() {
            while (true) {
                synchronized (object) {
                    try {
                        System.out.println(Thread.currentThread().getName() + " wait之前");
                        object.wait();
                        System.out.println(Thread.currentThread().getName() + " 被唤醒");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    static class T2 implements Runnable {
        @Override
        public void run() {
            while (true) {
                synchronized (object) {
                    object.notify();
                    System.out.println(Thread.currentThread().getName() + " 已notify，但是要等待t2用0.5S时间来释放锁");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("0.5S之后：");
                }
            }
        }
    }

    public static void main(String[] args) {
        T1 t1 = new T1();
        T2 t2 = new T2();

        Thread t11 = new Thread(t1, "t1");
        t11.setPriority(6);
        t11.start();

        Thread t22 = new Thread(t1, "t2");
        t22.setPriority(8);
        t22.start();

        new Thread(t2, "t3").start();
    }
    //t3 随机唤醒出t1或者t2
}
